package 富婆梦.算法题;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第六周.作业
 * @ClassName: Palindrome
 * @Author: hemengjin
 * @Description: 回文数算法类，可以识别回文数
 *               例：输入12321 是回文数 -12321 不是回文数
 * @Date:2021/8/28 18:44
 * @Version: 1.0
 */
public class Palindrome {
    public boolean judgePalindrome(int number) {
        //数字转成字符串
        String num = String.valueOf(number);
        //字符串转成字符数组
        char[] chars = num.toCharArray();
        int x = chars.length / 2;
        //count用于统计有几个对称的数是相等的
        int count = 0;
        for (int i = 0; i < x; i++) {
            if (chars[i] == chars[chars.length - i - 1]) {
                count++;
            }
        }
        if (count == x) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Palindrome p = new Palindrome();
        //true
        int a=121;
        //false
        int b=-121;
        //false
        int c=10;
        //true
        int d=1221;
        if (p.judgePalindrome(a)&&!p.judgePalindrome(b)&&!p.judgePalindrome(c)&&p.judgePalindrome(d)){
            System.out.println("测试通过！完成回文函数的编写");
        }else {
            System.out.println("测试失败！看看哪里还有逻辑问题！");
        }
        System.out.println("121是不是回文数："+p.judgePalindrome(a));
        System.out.println("-121是不是回文数："+p.judgePalindrome(b));
        System.out.println("10是不是回文数："+p.judgePalindrome(c));
        System.out.println("1221是不是回文数："+p.judgePalindrome(d));
    }
}
